package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址管理")
@Slf4j
public class AddressBookController {
	@Autowired
	private AddressBookService addressBookService;
	
	/**
	 * 新增地址
	 */
	@RequestMapping
	@ApiOperation("新增地址")
	public Result addAddress(@RequestBody AddressBook addressBook) {
		log.info("新增地址：{}", addressBook);
		addressBookService.addAddress(addressBook);
		return Result.success();
	}
	
	/**
	 * 查询所有地址
	 */
	@GetMapping("/list")
	@ApiOperation("查询所有地址")
	public Result<List<AddressBook>> getAddress() {
		log.info("查询所有地址");
		return Result.success(addressBookService.getAddress());
	}
	
	/**
	 * 查询默认地址
	 */
	@GetMapping("/default")
	@ApiOperation("查询默认地址")
	public Result<AddressBook> getDefault() {
		log.info("查询默认地址");
		return Result.success(addressBookService.getDefault());
	}
	
	/**
	 * 根据id查询地址
	 */
	@GetMapping("/{id}")
	@ApiOperation("根据id查询地址")
	public Result<AddressBook> getAddressById(@PathVariable Long id) {
		log.info("根据id查询地址：{}", id);
		return Result.success(addressBookService.getAddressById(id));
	}
	
	/**
	 * 根据id修改地址
	 */
	@PutMapping
	@ApiOperation("根据id修改地址")
	public Result updateAddress(@RequestBody AddressBook addressBook) {
		log.info("根据id修改地址：{}", addressBook);
		addressBookService.updateAddress(addressBook);
		return Result.success();
	}
	
	/**
	 * 修改默认地址
	 */
	@PutMapping("/default")
	@ApiOperation("修改默认地址")
	public Result putAddress(@RequestBody AddressBook addressBook) {
		log.info("修改默认地址：{}", addressBook);
		addressBookService.putDefault(addressBook);
		return Result.success();
	}
	
	/**
	 * 删除地址
	 */
	@DeleteMapping
	@ApiOperation("删除地址")
	public Result deleteAddress(Long id) {
		log.info("删除地址：{}", id);
		addressBookService.deleteAddress(id);
		return Result.success();
	}
}
